#include "PreprocessedFile.h"

#include <qfile.h>
#include <qdatastream.h>
#include <qtextstream.h>
#include <qstringlist.h>

#include <PointCloud.h>
#include <Camera.h>

#include <iostream>

using namespace std;

PreprocessedFile::PreprocessedFile()
{
}

void PreprocessedFile::load(const QString & filename)
{
    QFile file(filename);

    m_cameras.clear();
    m_point_cloud.clear();

    if (file.exists())
    {
        if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
        {
            cout << "target file couldn't be opened." << endl;
        }

        QTextStream in (&file);

        QString pointsStr = in.readLine();
        QStringList pointData;

        QList<Point3D> pointList;
        Point3D p;

        int points = pointsStr.toInt();

        for(int i=0; i < points; i++)
        {
            pointsStr = in.readLine();
            pointData = pointsStr.split(' ');

            p.setX(pointData.at(0).toFloat());
            p.setY(pointData.at(1).toFloat());
            p.setZ(pointData.at(2).toFloat());

            p.setColor(pointData.at(3).toFloat(),
                       pointData.at(4).toFloat(),
                       pointData.at(5).toFloat());

            pointList.append(p);
        }

        m_point_cloud.setPoints(pointList);

        QString cameraStr = in.readLine();
        QStringList cameraData;
        Camera camera;
        int cameraCount = cameraStr.toInt();

        for(int i=0; i < cameraCount; i++)
        {
            cameraStr = in.readLine();
            cameraData = cameraStr.split(' ');


            camera.setCameraPosition(Vector3D(cameraData.at(0).toFloat(), cameraData.at(1).toFloat(), cameraData.at(2).toFloat()));
            camera.setCameraDirection(Vector3D(cameraData.at(3).toFloat(), cameraData.at(4).toFloat(), cameraData.at(5).toFloat()));
            camera.setCameraRightVector(Vector3D(cameraData.at(6).toFloat(), cameraData.at(7).toFloat(), cameraData.at(8).toFloat()));
            camera.setCameraUpVector(Vector3D(cameraData.at(9).toFloat(), cameraData.at(10).toFloat(), cameraData.at(11).toFloat()));
            camera.setCameraFovy(cameraData.at(12).toFloat());
            camera.setCameraRotationVector(Vector3D(cameraData.at(13).toFloat(), cameraData.at(14).toFloat(), cameraData.at(15).toFloat()));
            camera.setFilename(cameraData.at(16));

            m_cameras.append(camera);
        }
    }

    file.close();
}

void PreprocessedFile::save(const QString & filename)
{
    QFile file(filename);

    file.open(QIODevice::WriteOnly);

    Point3D point;
    Camera camera;

    QTextStream out(&file);

    // Writing the size of point cloud
    out << (int) m_point_cloud.getPointCount() << '\n';

    // Writing the details of points
    for(int i=0; i < m_point_cloud.getPointCount(); i++)
    {
        point = m_point_cloud.getPoint(i);

        out << (float) point.getX() << ' '
            << (float) point.getY() << ' '
            << (float) point.getZ() << ' '
            << (float) point.getRed() << ' '
            << (float) point.getGreen() << ' '
            << (float) point.getBlue() << '\n';
    }

    // Writing the size of cameras
    out << (int) m_cameras.size() << '\n' ;

    // Writing the details of cameras
    for(int i=0; i < m_cameras.size(); i++)
    {
        camera = m_cameras[i];

        out << (float) camera.getCameraPosition().getX() << ' '
            << (float) camera.getCameraPosition().getY() << ' '
            << (float) camera.getCameraPosition().getZ() << ' '
            << (float) camera.getCameraDirection().getX() << ' '
            << (float) camera.getCameraDirection().getY() << ' '
            << (float) camera.getCameraDirection().getZ() << ' '
            << (float) camera.getCameraRightVector().getX() << ' '
            << (float) camera.getCameraRightVector().getY() << ' '
            << (float) camera.getCameraRightVector().getZ() << ' '
            << (float) camera.getCameraUpVector().getX() << ' '
            << (float) camera.getCameraUpVector().getY() << ' '
            << (float) camera.getCameraUpVector().getZ() << ' '
            << (float) camera.getCameraFovy() << ' '
            << (float) camera.getCameraRotationVector().getX() << ' '
                << (float) camera.getCameraRotationVector().getY() << ' '
                    << (float) camera.getCameraRotationVector().getZ() << ' '
            << camera.getFilename() << '\n';
    }

    file.close();
}
